<!--
 * @Author: Frane
 * @Date: 2020-06-28 14:05:00
 * @LastEditors: 範遠茂
 * @LastEditTime: 2020-07-06 15:36:59
 * @Description: 收货信息
--> 
<template>
  <div class="re">
    <van-nav-bar title="收货信息" left-text="返回" left-arrow @click-left="goback" />

    <!-- 有收货信息 -->
    <template v-if="AddressInfo.length!=0">
      <div class="receipt" v-for="(item,idx1) of AddressInfo" :key="idx1">
        <div class="top">
          <h3>{{item.consignee+" "+item.consignee_mobile}}</h3>
          <p>{{item.province_name+item.city_name+item.area_name+item.consignee_address}}</p>
        </div>
        <van-divider />
        <div class="bottom">
          <van-checkbox class="bo1" v-model="item.is_default" @click="isdefault(idx1)">设为默认地址</van-checkbox>
          <div class="bo2">
            <div class="edit">
              <router-link :to="{path:'/editAddress',query:{id:item.id}}">
                <van-icon name="edit" />
                <p class="ed">编辑</p>
              </router-link>
            </div>
            <div class="delete" @click="del(idx1)">
              <van-icon name="delete" />
              <p class="de">删除</p>
            </div>
          </div>
        </div>
      </div>
    </template>
    <!-- 没有收货信息 -->
    <template v-else>
      <div class="message">
        <van-icon class="icon" name="shopping-cart-o"></van-icon>
        <span class="text">暂无收货地址</span>
      </div>
    </template>

    <router-link :to="{path:'/addAddress'}">
      <van-button round type="danger" size="large" class="btn">+添加新收件地址</van-button>
    </router-link>
  </div>
</template>

<script>
import * as api from "@/api/apiList/user/address/address.js";
export default {
  data() {
    return {
      AddressInfo: [], //地址列表数据
    };
  },

  methods: {
    async API_get_address() {
      //获取收货地址列表
      return await api.get_address();
    },
    async API_del_address(id) {
      //删除地址
      let params = {
        "id[]": id
      };
      return await api.ajaxdel(params);
      // console.log(params)
    },
    async API_isdefault(id) {
      return await api.default_address(id);
    },
    goback() {
      //返回上一頁
      this.$router.push("/my");
    },
    del(idx1) {
      //刪除地址
      this.$dialog
        .confirm({
          title: "删除地址",
          message: "是否要删除该地址？"
        })
        .then(async () => {
          let res2 = await this.API_del_address(this.AddressInfo[idx1].id);
          if (res2.code == 400) {
            this.$toast(res2.message);
          }
          this.API_get_address();
          this.$router.go(0);
        })
        .catch(() => {});
    },

    async isdefault(idx1) {
      //设为默认
      let res3 = await this.API_isdefault(this.AddressInfo[idx1].id);
      console.log(res3);
      this.$router.go(0)
      
    }
  },
  async created() {
    let res = await this.API_get_address();
    if (res.data.code === 200) {
      this.AddressInfo = res.data.data.data;
    }
  }
};
</script>

<style lang="scss" scoped>
@import "@/style/receipt/receipt";
</style>

